package homework8_2;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtil {
	static Connection con = null;
	static Statement stm = null;
	static String sql = null;
	static ResultSet re = null;

	public static String delete(int id) {
		try {
			stm = con.createStatement();
			sql = "delete from s_subject where id=64 ";
			int r = stm.executeUpdate(sql);
			System.out.println(String.format("删除了%d条", r));

		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}
		return sql;

	}

	public static String select(int id) {
		try {
			stm = con.createStatement();
			String sql = "select * from s_grade where id=10";
			re = stm.executeQuery(sql);
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				re.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			try {
				stm.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}
		return sql;

	}

	public static String update(int id, String sname) {
		try {
			stm = con.createStatement();
			sql = "update s_student set sname =" + sname + " where stuno =" + id + "";
			int r = stm.executeUpdate(sql);
			System.out.println(String.format("删除了%d条", r));

		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}
		return sql;

	}
}
